package ${configure.packageName}.${generatorTable.moudleName}.controller.${generatorTable.fileDirName};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.validation.annotation.Validated;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;

import ${configure.packageName}.${generatorTable.moudleName}.service.${generatorTable.fileDirName}.${generatorTable.upperClassName}Service;
import ${configure.packageName}.${generatorTable.moudleName}.model.${generatorTable.fileDirName}.${generatorTable.upperClassName};

import javax.annotation.Resource;

@Controller
@RequestMapping("/${generatorTable.moudleName}/${generatorTable.shiro}")
public class ${generatorTable.upperClassName}Action extends BaseAction {

	@Resource
	private ${generatorTable.upperClassName}Service ${generatorTable.className}Service;

	@GetMapping("/list")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:list")
	public String list(Model model) {
		<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
			<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
			</#list>
		</#if>
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirNameSeparator}/${generatorTable.shiro}/${generatorTable.className}-list";
	}

	@GetMapping("/add/input")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:add")
	public String addInput(Model model) throws Exception {
		<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
			<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
			</#list>
		</#if>
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "${generatorTable.moudleName}:${generatorTable.shiro}:add")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-保存${generatorTable.tableComment!""}")
	public AR save(@Validated(Insert.class) ${generatorTable.upperClassName} ${generatorTable.className}) throws Exception {
		return ${generatorTable.className}Service.save(${generatorTable.className});
	}

	@GetMapping("/edit/input")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:edit")
	public String updateInput(@Validated(Detail.class) ${generatorTable.upperClassName} ${generatorTable.className}, Model model) throws Exception {
		<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
			<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
			</#list>
		</#if>
		model.addAttribute("${generatorTable.className}", ${generatorTable.className}Service.getById(${generatorTable.className}.getId()));
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "${generatorTable.moudleName}:${generatorTable.shiro}:edit")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-更新${generatorTable.tableComment!""}")
	public AR edit(@Validated(Edit.class) ${generatorTable.upperClassName} ${generatorTable.className}) throws Exception {
		return ${generatorTable.className}Service.update(${generatorTable.className});
	}


	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:list")
	public String listInfo(${generatorTable.upperClassName} ${generatorTable.className}, Page page) throws Exception {
		return JSONData.toLayerData(${generatorTable.className}Service.list(page , new QueryWrapper(${generatorTable.className}),
	<#if generatorTable.modelInherits != 'BaseBean'>
				true));
	</#if>
	<#if generatorTable.modelInherits == 'BaseBean'>
				null));
	</#if>
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:del")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-删除${generatorTable.tableComment!""}")
	public AR deleteById(String id) throws Exception{
		return ${generatorTable.className}Service.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:del")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-批量删除${generatorTable.tableComment!""}")
	public AR deleteByIds(String ids) throws Exception {
		return ${generatorTable.className}Service.deleteBatchIds(ids);
	}

	@GetMapping("select")
	public String select(Model model) {
		<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
			<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
			</#list>
		</#if>
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-select";
	}
}
